//
//  DiscoAndProController.m
//  MBObjectServer
//
//  Created by WorkSpace on 4/2/14.
//  Copyright (c) 2014 MBpayer. All rights reserved.
//

#import "DiscoAndProController.h"

@implementation DiscoAndProController

#pragma mark 整单-单品折扣
-(void)DoDiscount:(NSMutableArray *)detailEntitys
             IsWholeOrder:(NSNumber *)isWholeOrder
             DiscountRate:(NSNumber *)discountRate
            DiscountPrice:(NSNumber *)discountPrice
                     Mode:(NSNumber *)mode
           DisOnBillPrice:(NSNumber *)disOnBillPrice
                   Remark:(NSString *)remark
               RemarkCode:(NSString *)remarkCode
                  Success:(ReceiveSuccessDis)receiveSuccessDis
                  Failure:(ReceiveFailure)processFailure
{
    NSMutableDictionary *postDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:detailEntitys,@"DetailEntitys",isWholeOrder,@"IsWholeOrder",discountRate,@"DiscountRate",discountPrice,@"DiscountPrice",mode ,@"Mode",disOnBillPrice,@"DisOnBillPrice",remark,@"Remark",remarkCode,@"RemarkCode",nil];
    NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObject:postDic forKey:@"DiscountWrap"];
    
    MBBaseServer *baseServer = [[MBBaseServer alloc]init];
    [baseServer setRequestStyle:nil andSetPathString:@"billsale_handler.ashx" andSetHeadString:nil andActionName:@"DoDiscount" andSetMainDictionary:dic2 block:^(id json)
    {
        discountmodel = [[DiscountMesModel alloc] init];
        discountmodel.DiscountRate = (NSNumber *)[json objectForKey:@"DiscountRate"];
//        discountmodel.IsWholeOrder = (NSNumber *)[json objectForKey:@"IsWholeOrder"];
//        discountmodel.IsWholeOrder = [NSNumber numberWithBool:YES];
        discountmodel.DiscountPrice = (NSNumber *)[json objectForKey:@"DiscountPrice"];
        NSMutableArray * detailentityArray = [json objectForKey:@"DetailEntitys"];
        receiveSuccessDis(discountmodel,detailentityArray);
    }
    failblock:^(NSError *error)
    {
        processFailure(error);
    }];
}
#pragma mark 打折券
-(void)VerifyTicketForClient:(NSString *)ticketNo
                   CardNo:(NSString *)cardNo
                  Success:(ReceiveSuccessCcC)receiveSuccessCcC
                  Failure:(ReceiveFailure)processFailure
{
    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
    NSString * vipCode = [userDefault objectForKey:kVIPCode];
    NSMutableDictionary *postDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[userDefault objectForKey:kOrgCode],@"OrgCode",ticketNo,@"TicketNo",vipCode,@"CardNo",nil];
    
    MBBaseServer *baseServer = [[MBBaseServer alloc]init];
    [baseServer setRequestStyle:nil andSetPathString:@"vipuser_handler.ashx" andSetHeadString:nil andActionName:@"VerifyTicketForClient" andSetMainDictionary:postDic block:^(id json)
     {
        discountTicket = [EPOSDiscountTicket getEPOSDiscountTicketFromDictionary:json];
        ccCouponCardInfo = [EPOSCcCouponCardInfo getEPOSCcCouponCardInfoFromDictionary:discountTicket.CouponEntity];
         receiveSuccessCcC(ccCouponCardInfo);
     }
     failblock:^(NSError *error)
     {
         processFailure(error);
     }];
}
#pragma mark 促销获取
-(void)RetrievePromtionRules:(ReceiveSuccessPro)receiveSuccessPro
                     Failure:(ReceiveFailure)processFailure
{
    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults] ;
    NSMutableDictionary *postDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[userDefault objectForKey:kOrgCode],@"OrgCode",[userDefault objectForKey:kOwnerOrgCode],@"OwnerOrgCode",[userDefault objectForKey:kCashierID],@"CashierID",nil];
    MBBaseServer *baseServer = [[MBBaseServer alloc]init];
    [baseServer setRequestStyle:nil andSetPathString:@"billsale_handler.ashx" andSetHeadString:nil andActionName:@"RetrievePromtionRules" andSetMainDictionary:postDic block:^(id json)
     {
         promotionModelResult = [PromotionModelResult getEPOSBaseResultFromDictionary:json];
         receiveSuccessPro(promotionModelResult);
     }
     failblock:^(NSError *error)
     {
         processFailure(error);
     }];
}
#pragma mark 促销更新
-(void)UpdatePromotStatus:(NSMutableArray *)promotionRuleSelects
                  Success:(ReceiveSuccessPro)receiveSuccessPro
                  Failure:(ReceiveFailure)processFailure
{
    NSMutableArray *postModelArr = [[NSMutableArray alloc]init];
    for (int i = 0 ; i<promotionRuleSelects.count; i++)
    {
        PromotionModel *getDic = [[PromotionModel alloc]init];
        getDic = [promotionRuleSelects objectAtIndex:i];
        NSMutableDictionary *postModelDic = [PromotionModel getDictionaryFromEPromotionModel:getDic];
        
        if ([[postModelDic objectForKey:@"IsSelected"]boolValue]==YES)
        {
            MBLog(@"ID = %@",[postModelDic objectForKey:@"PromotionRuleid"]);
            NSMutableDictionary *pot = [[NSMutableDictionary alloc]init];
            [pot setObject:[postModelDic objectForKey:@"PromotionRuleid"] forKey:@"PromotionRuleid"];
            NSNumber *i = [NSNumber numberWithInt:[[postModelDic objectForKey:@"IsSelected"]intValue]];
            [pot setObject:i forKey:@"IsSelected"];
            [postModelArr addObject:pot];
        }
    }
    
    
    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary *postUpdata = [NSMutableDictionary dictionaryWithObjectsAndKeys:[userDefault objectForKey:kOrgCode],@"OrgCode",postModelArr,@"RuleSelects",[userDefault objectForKey:kCashierID],@"CashierID",nil];

    MBBaseServer *baseServer = [[MBBaseServer alloc]init];
    [baseServer setRequestStyle:nil andSetPathString:@"billsale_handler.ashx" andSetHeadString:nil andActionName:@"UpdatePromotStatus" andSetMainDictionary:postUpdata block:^(id json)
     {
         promotionModelResult = [PromotionModelResult getEPOSBaseResultFromDictionary:json];
         receiveSuccessPro(promotionModelResult);
     }
     failblock:^(NSError *error)
     {
         processFailure(error);
     }];
}


@end
